//
//  LYCVisitorView.swift
//  新浪微博
//
//  Created by liuyanchi on 16/7/28.
//  Copyright © 2016年 liuyanchi. All rights reserved.
//

import UIKit

import SnapKit

// 设置代理
protocol LYCVisitorViewDelegate:NSObjectProtocol {

    // 设置代理方法
    func didRegisterMethod()
    func didLoginMethod()
    

}


class LYCVisitorView: UIView {
    
    // 设置代理属性
    weak var delegate: LYCVisitorViewDelegate?
    
    
    // 第一步:重写父类的构造函数
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    // 判断sb是否执行
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二部:定义一个添加子类和何止子类控件的约束的方法
    
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        // 添加到视图
        
        addSubview(turnImageView)
        addSubview(maskImageView)
        addSubview(homeIconImageView)
        
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置约束
        
        // 蒙版的约束
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
            
        }
        // 小房子
        homeIconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
            
        }
        
        // 转圈
        turnImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
            
        }
        
        // 提示
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(turnImageView.snp_bottom).offset(10)
            make.width.equalTo(216)
            
            // 注册按钮
            registerButton.snp_makeConstraints(closure: { (make) -> Void in
                
                make.left.equalTo(messageLabel.snp_left)
                make.top.equalTo(messageLabel.snp_bottom).offset(10)
                make.width.equalTo(100)
                make.height.equalTo(30)
                
                // 登陆按钮
                loginButton.snp_makeConstraints(closure: { (make) -> Void in
                    
                    make.right.equalTo(messageLabel.snp_right)
                    make.top.equalTo(registerButton.snp_top)
                    make.width.equalTo(registerButton.snp_width)
                    make.height.equalTo(registerButton.snp_height)
                })
                
            })
            
        }
        
    }
    
    // MARK:-- 首页的动画效果
    func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        // 转的圈数
        animation.toValue = 2 * M_PI
        
        // 次数和时间
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        
        // 当我们切换视图时,动画效果会被一出,所以要手动的进不移除设置
        animation.removedOnCompletion = false
        
        // 添加到layer里
        turnImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    // MARK:-- 对外提供一个接口,改变视图
    func setupInfo(imageName: String,message: String,isHome:Bool = false){
        
        homeIconImageView.image = UIImage(named: imageName)
        
        messageLabel.text = message
        
        if isHome {
            
            turnImageView.hidden = false
            
            // 调用动画效果
            startAnimation()
            
        }else{
            
            turnImageView.hidden = true
            
        }
        
        
        
    }
    
    
    
    
    // 注册按钮的点击事件
    func register(){
        
        print("注册了")
        
        // 调用调离方法
         delegate?.didRegisterMethod()
    
        
          }
    
    func login(){
        
        print("登陆了")
        
    }
    
    // MARK:-- 懒加载
    
    // 蒙版
    private lazy var maskImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return img
        
    }()
    
    // 小房子
    private lazy var homeIconImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
        
    }()
    
    // 转动图片
    private lazy var turnImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
        
    }()
    
    // 提示文字
    private lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人,回到这里看看有什么惊喜~~"
        label.font = UIFont.systemFontOfSize(14.0)
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = .Center
        
        return label
        
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 设置背景
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
        // 设置点击事件
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn
        
    }()
    
    // 登陆按钮
    private lazy var loginButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 设置背景
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        
        // 设置点击事件
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn
        
    }()
    
    
    
}
